import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { getGuests, createGuest, updateGuest, deleteGuest } from '@/api/guest'
import { Message } from '@arco-design/web-vue'

export const useGuestStore = defineStore('guest', () => {
  // 状态
  const guests = ref([])
  const loading = ref(false)
  const pagination = ref({
    current: 1,
    size: 10,
    total: 0
  })
  const filters = ref({
    search: '',
    phone: '',
    vip_level: ''
  })

  // 计算属性
  const vipLevelOptions = computed(() => [
    { label: '白银会员', value: 0 },
    { label: '黄金会员', value: 1 },
    { label: '白金会员', value: 2 },
    { label: '钻石会员', value: 3 }
  ])

  // 获取客户列表
  const fetchGuests = async (params = {}) => {
    loading.value = true
    try {
      const data = await getGuests({
        page: pagination.value.current,
        size: pagination.value.size,
        ...filters.value,
        ...params
      })
      
      console.log('获取到客户数据:', data)
      
      // 后端返回的是 list 而不是 guests
      guests.value = data.list || data.guests || []
      pagination.value.total = data.total || 0
      pagination.value.current = data.page || data.current_page || 1
      
      console.log('客户列表已更新:', guests.value.length, '条记录')
    } catch (error) {
      console.error('获取客户列表失败:', error)
      guests.value = []
    } finally {
      loading.value = false
    }
  }

  // 创建客户
  const createGuestData = async (guestData) => {
    try {
      await createGuest(guestData)
      Message.success('客户创建成功')
      await fetchGuests()
      return true
    } catch (error) {
      console.error('创建客户失败:', error)
      return false
    }
  }

  // 更新客户
  const updateGuestData = async (id, guestData) => {
    try {
      await updateGuest(id, guestData)
      Message.success('客户更新成功')
      await fetchGuests()
      return true
    } catch (error) {
      console.error('更新客户失败:', error)
      return false
    }
  }

  // 删除客户
  const deleteGuestData = async (id) => {
    try {
      await deleteGuest(id)
      Message.success('客户删除成功')
      await fetchGuests()
      return true
    } catch (error) {
      console.error('删除客户失败:', error)
      return false
    }
  }

  // 设置筛选条件
  const setFilters = (newFilters) => {
    filters.value = { ...filters.value, ...newFilters }
    pagination.value.current = 1
  }

  // 设置分页
  const setPagination = (newPagination) => {
    pagination.value = { ...pagination.value, ...newPagination }
  }

  // 重置筛选条件
  const resetFilters = () => {
    filters.value = {
      search: '',
      phone: '',
      vip_level: ''
    }
    pagination.value.current = 1
  }

  return {
    // 状态
    guests,
    loading,
    pagination,
    filters,
    
    // 计算属性
    vipLevelOptions,
    
    // 方法
    fetchGuests,
    createGuestData,
    updateGuestData,
    deleteGuestData,
    setFilters,
    setPagination,
    resetFilters
  }
})
